<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单流水号" prop="orderSerial">
        <el-input v-model="queryParams.orderSerial" placeholder="请输入订单流水号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="快递公司" prop="emsCom">
        <el-select v-model="queryParams.emsCom" placeholder="请选择快递公司" clearable>
          <el-option v-for="dict in dict.type.com" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="快递单号" prop="emsNum">
        <el-input v-model="queryParams.emsNum" placeholder="请输入快递单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发货地" prop="emsFrom">
        <el-input v-model="queryParams.emsFrom" placeholder="请输入发货地" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="目的地" prop="emsTo">
        <el-input v-model="queryParams.emsTo" placeholder="请输入目的地" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物流状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择物流状态" clearable>
          <el-option v-for="dict in dict.type.ems_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="状态更新时间" prop="emsTime">
        <el-date-picker clearable v-model="queryParams.emsTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择状态更新时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="创建时间" prop="crTime">
        <el-date-picker clearable v-model="queryParams.crTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="upTime">
        <el-date-picker clearable v-model="queryParams.upTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" :disabled="multiplePl"
          @click="handleDeliveryList" v-hasPermi="['store:ems:deliveryList']">批量发货</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['store:ems:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['store:ems:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['store:ems:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['store:ems:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="emsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="快递信息id" align="center" prop="emsId" width="120" />
      <el-table-column label="订单流水号" align="center" prop="orderSerial" width="120" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="商品名称" align="center" prop="productName" /><el-table-column label="商品图片" align="center"
        prop="productPic" width="120">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.productPic" :fit="fit"></el-image>
        </template>
      </el-table-column><!-- <el-table-column label="商品图片" align="center" prop="productPic" /> -->
      <el-table-column label="快递公司" align="center" prop="emsCom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.com" :value="scope.row.emsCom" />
        </template>
      </el-table-column>
      <el-table-column label="快递单号" align="center" prop="emsNum" width="120" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="发货地" align="center" prop="emsFrom" width="120" />
      <el-table-column label="目的地" align="center" prop="emsTo" width="180" />
      <el-table-column label="物流状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ems_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="快递内容" show-overflow-tooltip="true" align="center" prop="context" /> -->
      <el-table-column label="状态更新时间" align="center" prop="emsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.emsTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="轨迹地图链接" align="center" prop="trailUrl" /> -->
      <el-table-column label="预计到达时间" align="center" prop="arrivalTime" width="180" />
      <el-table-column label="到达还需多少时间" align="center" prop="remainTime" width="180" />
      <el-table-column label="创建时间" align="center" prop="crTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.crTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="upTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['store:ems:edit']">发货</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['store:ems:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 商家发货对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="订单流水号" prop="orderSerial">
          <el-input v-model="form.orderSerial" placeholder="请输入订单流水号" :disabled="true" />
        </el-form-item>
        <el-form-item label="快递公司" prop="emsCom">
          <el-select v-model="form.emsCom" placeholder="请选择快递公司">
            <el-option v-for="dict in dict.type.com" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="emsNum">
          <el-input v-model="form.emsNum" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="发货地" prop="emsFrom">
          <el-input v-model="form.emsFrom" placeholder="请输入发货地" />
        </el-form-item>


        


        <el-form-item label="发货人" prop="fromUser">
          <el-input v-model="form.fromUser" placeholder="请输入发货人" />
        </el-form-item>
        <el-form-item label="发货人手机号" prop="fromPhone">
          <el-input v-model="form.fromPhone" placeholder="请输入发货人手机号" />
        </el-form-item>
        <el-form-item label="收货人" prop="toUser">
          <el-input v-model="form.toUser" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="收货手机号" prop="toPhone">
          <el-input v-model="form.toPhone" placeholder="请输入收货手机号" />
        </el-form-item>

        <el-form-item label="预计取件时间" prop="startDate">
          <el-date-picker
      v-model="pickUpTime"
      value-format="yyyy-MM-dd HH:mm:ss"
     type="datetime"
      placeholder="选择日期">
    </el-date-picker>
        </el-form-item>
        <el-form-item label="最后取件时间" prop="overDate">
          <el-date-picker
      v-model="pickUpTimeEnd"
      value-format="yyyy-MM-dd HH:mm:ss"
      type="datetime"
      placeholder="选择日期">
    </el-date-picker>
        </el-form-item>



        <el-form-item label="目的地" prop="emsTo">
          <el-input v-model="form.emsTo" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="物流状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择物流状态">
            <el-option v-for="dict in dict.type.ems_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog><el-dialog :title="titlePl" :visible.sync="openPl" width="500px" append-to-body>
      <el-form ref="formPl" :model="formPl" :rules="rulesPl" label-width="80px">
        <el-form-item label="订单流水号" prop="orderSerial">
          <el-input v-model="formPl.orderSerial" placeholder="请输入订单流水号" :disabled="true" />
        </el-form-item>
        <el-form-item label="快递公司" prop="emsCom">
          <el-select v-model="formPl.emsCom" placeholder="请选择快递公司">
            <el-option v-for="dict in dict.type.com" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="emsCom">
          <div v-for="(item,idx) in productIds" :key="idx">
            <el-image style="width: 100px; height: 100px" :src="item" :fit="fit"></el-image>
          </div>
        </el-form-item>
        <el-form-item label="快递单号" prop="emsNum">
          <el-input v-model="formPl.emsNum" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="发货地" prop="emsFrom">
          <el-input v-model="formPl.emsFrom" placeholder="请输入发货地" />
        </el-form-item>
        <!-- :disabled="true" -->
        <el-form-item label="手机号" prop="phone">
          <el-input  v-model="formPl.phone" placeholder="请输入手机号" />
        </el-form-item>
        <!-- :disabled="true"  -->
        <el-form-item label="目的地" prop="emsTo">
          <el-input v-model="formPl.emsTo" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="物流状态" prop="status">
          <el-select :disabled="true" v-model="formPl.status" placeholder="请选择物流状态">
            <el-option v-for="dict in dict.type.ems_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormPl">确 定</el-button>
        <el-button @click="cancelpl">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listEms,
    getEms,
    delEms,
    addEms,
    updateEms,
    deliveryList
  } from "@/api/store/ems";

  export default {
    name: "Ems",
    dicts: ['ems_status', 'com'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 快递信息表格数据
        emsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderSerial: null,
          emsCom: null,
          emsNum: null,
          phone: null,
          emsFrom: null,
          emsTo: null,
          status: null,
          context: null,
          emsTime: null,
          trailUrl: null,
          arrivalTime: null,
          remainTime: null,
          crTime: null,
          upTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        multiplePl: true,
        titlePl: '批量发货',
        openPl: false,
        formPl: {},
        // 表单校验
        rulesPl: {},
        productIds: [],
        orderSerialIds: [],
        pickUpTime:"",
pickUpTimeEnd:'',
      };
    },
    created() {
      this.getList();
    },
    methods: {
      areValuesSame(arr) {
        return arr.every(element => element === arr[0]);
      },
      handleDeliveryList() {
        //如果两个数据不一致
        if (this.orderSerialIds.length > 1) {
          let res = this.areValuesSame(this.orderSerialIds)
          if (!res) {
            this.$modal.msgSuccess("订单流水号不同，不允许批量发货");
            return
          }
        }
        this.openPl = true
      },
      /** 查询快递信息列表 */
      getList() {
        this.loading = true;
        listEms(this.queryParams).then(response => {
          this.emsList = response.data;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          emsId: null,
          orderSerial: null,
          storeName: null,
          productName: null,
          productPic: null,
          emsCom: null,
          emsNum: null,
          phone: null,
          emsFrom: null,
          emsTo: null,
          status: null,
          context: null,
          emsTime: null,
          trailUrl: null,
          arrivalTime: null,
          remainTime: null,
          delFlag: null,
          crTime: null,
          upTime: null,
          emsField1: null,
          emsField2: null,
          emsField3: null,
          emsField4: null,
          emsField5: null,

          fromUser: null,
          fromPhone: null,
          toUser: null,
          toPhone: null,
          startDate: null,
          overDate: null,
        };

//         <!--  发货人
//  发货人手机号
//  收货人
//  收货手机号

//  预计取件时间
//  最后取件时间 -->


        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.emsId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        if (this.ids.length > 0) {
          this.multiplePl = false
          this.formPl = selection[0]
          this.productIds = selection.map(item => item.productPic)
          this.orderSerialIds = selection.map(item => item.orderSerial)
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加快递信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const emsId = row.emsId || this.ids
        getEms(emsId).then(response => {
          this.historyStatus = response.data.status
          this.form = response.data;
          console.log(this.form)
          this.open = true;
          this.title = "修改快递信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        console.log(this.pickUpTime)
        console.log(this.pickUpTimeEnd)
        this.form.startDate = this.pickUpTime
        this.form.overDate = this.pickUpTimeEnd
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.emsId != null) {
              if (this.historyStatus != 15) {
                this.$modal.msgError("不允许重复发货");
                return
              }
              updateEms(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addEms(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const emsIds = row.emsId || this.ids;
        this.$modal.confirm('是否确认删除快递信息编号为"' + emsIds + '"的数据项？').then(function() {
          return delEms(emsIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },


      resetPl() {
        this.formPl = {
          emsId: null,
          orderSerial: null,
          storeName: null,
          productName: null,
          productPic: null,
          emsCom: null,
          emsNum: null,
          phone: null,
          emsFrom: null,
          emsTo: null,
          status: null,
          context: null,
          emsTime: null,
          trailUrl: null,
          arrivalTime: null,
          remainTime: null,
          delFlag: null,
          crTime: null,
          upTime: null,
          emsField1: null,
          emsField2: null,
          emsField3: null,
          emsField4: null,
          emsField5: null
        };
        this.resetForm("formPl");
      },

      // 批量发货
      submitFormPl() {
        let data = {
          "orderSerial": this.formPl.orderSerial,
          "emsIds": this.ids,
          "emsTo": this.formPl.emsTo,
          "emsCom": this.formPl.emsCom,
          "emsNum": this.formPl.emsNum,
          "phone": this.formPl.phone,
          "emsFrom": this.formPl.emsFrom,
          "emsPic": this.productIds.join(','),
          "emsNumb": this.ids.length
        }
        this.$refs["formPl"].validate(valid => {
          if (valid) {
            deliveryList(data).then(response => {
              this.$modal.msgSuccess("发货成功");
              this.openPl = false;
              this.getList();
            });
          }
        });
      },
      cancelpl() {
        this.openPl = false;
        this.resetPl();
      },
      /** 批量发货按钮 */
      //     handleDelete(row) {
      //   const emsIds = row.emsId || this.ids;
      //   this.$modal.confirm('是否确认删除快递信息编号为"' + emsIds + '"的数据项？').then(function() {
      //     return delEms(emsIds);
      //   }).then(() => {
      //     this.getList();
      //     this.$modal.msgSuccess("删除成功");
      //   }).catch(() => {});
      // },

      /** 导出按钮操作 */
      handleExport() {
        this.download('store/ems/export', {
          ...this.queryParams
        }, `ems_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
